package cn.itrip.auth.controller;

import cn.itrip.auth.service.TokenService;
import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.vo.ItripTokenVO;
import cn.itrip.common.DateUtil;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.ErrorCode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;

/**
 * @author 瑾
 *
 */
@Controller
@RequestMapping("/api")
public class TokenController {

    @Resource(name = "tokenService")
    private TokenService tokenService;
    /**
     * 验证token有效性
     * @param request
     * @headers 强制他必须在header里面提供token --KEY
     * @return
     */
    @RequestMapping(value = "/validateToken",method = RequestMethod.GET,produces = "application/json",headers = "token")
    @ResponseBody
    public Dto valitda(HttpServletRequest request){
        System.out.println("-----tokend----------");
        try {
            //验证后的结果
            boolean reuslt = this.tokenService.validate(request.getHeader("user-agent"),request.getHeader("token"));
            if(reuslt){
                //验证通过 token有效
                return DtoUtil.returnSuccess("token有效");
            }else {
                return DtoUtil.returnSuccess("token无效");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return  DtoUtil.returnFail(e.getMessage(), ErrorCode.AUTH_UNKNOWN);
        }
    }


    /**
     * 用户注销，删除token
     * @headers 强制他必须在header里面提供token --KEY --因为我们要验证token后 才能删除token
     */
    @RequestMapping(value = "/logout",method = RequestMethod.GET,produces = "application/json",headers = "token")
    @ResponseBody
    public Dto logout(HttpServletRequest request){
        System.out.println("--loginout---");
        //获得该token
        String token = request.getHeader("token");
        try {
            //验证token是否有效
            if(tokenService.validate(request.getHeader("user-agent"),token)){
                //token有效 才进行删除token
                tokenService.deleteToken(token);
                return  DtoUtil.returnSuccess();
            }else {
                //token 无效 则返回失败结果
                return  DtoUtil.returnFail("token无效",ErrorCode.AUTH_TOKEN_INVALID);

            }
        }catch (Exception e){
            e.printStackTrace();
            return  DtoUtil.returnFail("退出失败",ErrorCode.AUTH_TOKEN_INVALID);


        }

    }


    /**
     *  token 的置换  新老token的交替
     * @headers 强制他必须在header里面提供token --KEY
     */
    @RequestMapping(value = "/reToken",method = RequestMethod.POST,produces = "application/json",headers = "token")
    @ResponseBody
    public Dto reloadToken(HttpServletRequest request) {
        //1验证 2生 成新token 3 延长维护老token
        String token  = null;
        try {
            token = this.tokenService.reloadToken(request.getHeader("user-agent"),request.getHeader("token"));
            //返回token信息， 封装到vo里面
            //token字符串  过期时期  生成时间
            ItripTokenVO vo = new ItripTokenVO(token
                    , Calendar.getInstance().getTimeInMillis() + (2 * 60 * 60 * 1000)
                    , Calendar.getInstance().getTimeInMillis()
            );
            return DtoUtil.returnDataSuccess(vo);
        } catch (Exception e) {
            e.printStackTrace();
            return  DtoUtil.returnFail(e.getMessage(),ErrorCode.AUTH_UNKNOWN);
        }


    }

    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public @ResponseBody String test(){
        System.out.println("执行tetst-----");
       return null;
    }

}
